package controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cameraConfig.*;

public class CameraController extends HttpServlet {


	private static final long serialVersionUID = 1L;
	
	public void process ( HttpServletRequest request, HttpServletResponse response ) {
		ServletContext context;
		RequestDispatcher dispatcher;
		
		Vector<String> paramValues = new Vector<String>(Arrays.asList(request.getParameterValues("UserActionButton")));
		String curParamValue = null;
		
		KameraDAO camDAO = new KameraDAO();
		
		String error = null;				
		context = getServletContext();
		
		String dispatcherTarget = "/CameraListController";

		if((paramValues != null))
        {		
			//Relevanten Parameter Namen aus paramNames rausholen
			for (String string : paramValues)
			{
				curParamValue = string;
				if (curParamValue.contains("Btn")){
					break;}//For-schleife beenden
				curParamValue = null;
			}

		
			//Standort oder Kamera
				if(curParamValue.contains("loc")){
				//Es dreht sich um Standort
					//Edit oder New
					if(curParamValue.contains("Edit")){
						//Edit Standort
						dispatcherTarget = "/cameraConfigEditLocation.jsp";
						//Fehlerbehandlung
						Standort editLoc = camDAO.getLocationByName(curParamValue.substring(curParamValue.indexOf('_')+1));
						request.setAttribute("locationToEdit", editLoc);
					}else if(curParamValue.contains("New")){
						//New Standort
						dispatcherTarget = "/cameraConfigNewLocation.jsp";
					}
				}else if (curParamValue.contains("cam")){
				//Es dreht sich um Kamera
					//Edit oder New
					if(curParamValue.contains("Edit")){
						//Edit Kamera
						dispatcherTarget = "/cameraConfigEditCam.jsp";
						int ID = Integer.valueOf(curParamValue.substring(curParamValue.indexOf('_')+1)); // ID der Kamera aus dem String holen
						//request.setAttribute("camID",ID);
						//Fehlerbehandlung
						Kamera editCam = camDAO.getCamByID(ID);
						request.setAttribute("camToEdit", editCam);
						request.setAttribute("camLocation",editCam.getStandort());
					}else if(curParamValue.contains("New")){
						//New Kamera
						dispatcherTarget = "/cameraConfigNewCam.jsp";
						request.setAttribute("camLocation",curParamValue.substring(curParamValue.indexOf('_')+1) );
					}
				}
			
		
			if (error != null && !error.isEmpty())
				request.setAttribute("errorMessage", error);
			
			dispatcher = context.getRequestDispatcher(dispatcherTarget); // je nachdem, CamConfig, KameraForm oder StandortForm
					
			try {
				dispatcher.forward(request, response);
			} 
			catch (ServletException e) {
				e.printStackTrace();
				
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
        }

	}

	public void doGet ( HttpServletRequest request, HttpServletResponse response ) {
		process(request, response);
	}
	
	public void doPost ( HttpServletRequest request, HttpServletResponse response ) {
		process(request, response);
	}
}
